package com.banciyuan.bcywebview.base.view.pulltorefresh;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

/**
 * Created by squall on 2016/12/16.
 */

public class PullToRefreashRecycleView extends PullToRefreshBase<RecyclerView> {


    public PullToRefreashRecycleView(Context context) {
        super(context);
    }

    public PullToRefreashRecycleView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PullToRefreashRecycleView(Context context, Mode mode) {
        super(context, mode);
    }

    public PullToRefreashRecycleView(Context context, Mode mode, AnimationStyle animStyle) {
        super(context, mode, animStyle);
    }

    @Override
    public Orientation getPullToRefreshScrollDirection() {
        return Orientation.VERTICAL;
    }

    @Override
    protected RecyclerView createRefreshableView(Context context, AttributeSet attrs) {
        return new RecyclerView(context, attrs);
    }

    @Override
    protected boolean isReadyForPullEnd() {
        return false;
    }

    @Override
    protected boolean isReadyForPullStart() {
        if (mRefreshableView.getLayoutManager() instanceof LinearLayoutManager) {
            LinearLayoutManager layoutManager = (LinearLayoutManager) mRefreshableView.getLayoutManager();
            int visibleItemCount = layoutManager.getChildCount();
            int status = mRefreshableView.getScrollState();
            int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();

            if ((visibleItemCount > 0 && status == RecyclerView.SCROLL_STATE_IDLE && firstVisibleItemPosition == 0)) {
                int top = mRefreshableView.getLayoutManager().getChildAt(0).getTop() - mRefreshableView.getPaddingTop();
                if (top == 0) {
                    return true;
                }
            }
            return false;
        } else {
            return false;
        }
    }
}
